package variabilitysmelldetection.measurement.models;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import variabilitysmelldetection.measurement.measures.Measure;



public class ComponentItem extends Item {
	private boolean isInterface;
	private String superClass;
	private List<MethodItem> methods;
	private List<AttributeItem> attributes;
	
	public ComponentItem(String featureName, String classFileName) {
		super(featureName, classFileName);
		this.isInterface = false;
		superClass = "";
		methods = new ArrayList<MethodItem>();
		attributes = new ArrayList<AttributeItem>();
	}

	public ComponentItem(String featureName, String className, boolean isInterface) {
		this(featureName, className);
		this.isInterface = isInterface;
	}
	
	public boolean isInterface() {
		return isInterface;
	}

	public void setInterface(boolean isInterface) {
		this.isInterface = isInterface;
	}

	public String getSuperClass() {
		return superClass;
	}
	
	public void setSuperClass(String superClass) {
		this.superClass = superClass;
	}
	
	public List<MethodItem> getMethods() {
		return methods;
	}

	public void addMethod(MethodItem item) {
		this.methods.add(item);
	}
	
	public boolean hasMethods(){
		return !methods.isEmpty();
	}

	public List<AttributeItem> getAttributes() {
		return attributes;
	}

	public void addAttribute(AttributeItem item) {
		this.attributes.add(item);
	}
	
	public boolean hasAttributes(){
		return !attributes.isEmpty();
	}
	
	@Override
	public String getFieldsAndMeasuresNames() {
		StringBuilder dados = new StringBuilder();
		dados.append("Feature");
		dados.append(";");
		dados.append("Component");
		dados.append(";");
		dados.append("Interface");
		dados.append(";");
		dados.append("Refinement");
		dados.append(";");
		for (Measure m : getMeasures()) {
			dados.append(m.getAcronym());
			dados.append(";");
		}
		
		return dados.toString();
	}
	
	public String toCSV() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(getParentName());
		buffer.append(";");
		buffer.append(getName());
		buffer.append(";");
		buffer.append(isInterface ? "Y" : "N");
		buffer.append(";");
		buffer.append(isRefinement() ? "Y" : "N");
		buffer.append(";");
		for (Measure m : getMeasures()) {
			buffer.append(NumberFormat.getNumberInstance().format(m.getValue()));
			buffer.append(";");
		}
		
		return buffer.toString();
	}
	
	public String toString(){
		return getParentName()+"/"+getName();
	}

}